﻿using furion.blog.Application.Contracts;
using furion.blog.Application.CustomMapper;
using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Query.TagConfig;
using furion.blog.Domain.Shared;
using furion.blog.Domain.Shared.Dto.TagConfig;
using furion.blog.Domain.Shared.Enum;
using furion.blog.Domain.Shared.Vo.TagConfig;
using furion.blog.Utils;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Application
{
    public class TagConfigService : ITagConfigService, ISingleton
    {
        private readonly ITagConfigRepository _tagConfigRepository;
        private readonly TagConfigMapper _tagConfigMapper;

        public TagConfigService(ITagConfigRepository tagConfigRepository,TagConfigMapper tagConfigMapper) {
            this._tagConfigRepository = tagConfigRepository;
            this._tagConfigMapper = tagConfigMapper;
        }

        /// <summary>
        /// 标签分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> List(TagConfigListQuery query) {
            var ret = await this._tagConfigRepository.List(query);
            return ret;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task Delete(long id, long userId) {
            TagConfig tagConfig = await this._tagConfigRepository.FindById(id);
            if (tagConfig == null) {
                throw Oops.Oh("信息不存在");
            }
            tagConfig.IsDelete = YesOrNoEnum.Yes.ToString();
            tagConfig.UpdatedBy = userId;
            tagConfig.UpdatedTime = DateTime.Now;
            await this._tagConfigRepository.Update(tagConfig);
        }


        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task ChangeStatus(long id, string status, long userId) {
            TagConfig tagConfig = await this._tagConfigRepository.FindById(id);
            if (tagConfig == null)
            {
                throw Oops.Oh("信息不存在");
            }
            tagConfig.Status = status;
            tagConfig.UpdatedBy = userId;
            tagConfig.UpdatedTime = DateTime.Now;
            await this._tagConfigRepository.Update(tagConfig);
        }


        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TagConfigDetailVO> Detail(long id) {
            TagConfig tagConfig = await this._tagConfigRepository.FindById(id);
            if (tagConfig == null)
            {
                return null;
            }
            return tagConfig.Adapt<TagConfigDetailVO>();
        }


        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task Add(TagConfigCreateDto dto) {
            TagConfig tagConfig = await this._tagConfigRepository.First((new TagConfigDetailQuery { TagLabel = dto.TagLabel,GroupKey = dto.GroupKey }).Exp());
            if (tagConfig != null) {
                throw Oops.Oh("标签已存在");
            }
            tagConfig = dto.Adapt<TagConfig>();
            tagConfig.TagId = IdUtil.Id();
            await this._tagConfigRepository.Save(tagConfig);
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task Update(TagConfigUpdateDto dto) {
            TagConfig tagConfig = null;
            //查找标签是否已存在
            tagConfig = await this._tagConfigRepository.First((new TagConfigDetailQuery { TagIdNeq = dto.TagId, TagLabel = dto.TagLabel, GroupKey = dto.GroupKey }).Exp());
            if (tagConfig != null)
            {
                throw Oops.Oh("标签已存在");
            }
            //查找标签是否已存在
            tagConfig = await this._tagConfigRepository.First((new TagConfigDetailQuery { TagId = dto.TagId }).Exp());
            if (tagConfig == null)
            {
                throw Oops.Oh("数据不存在");
            }
            this._tagConfigMapper.Map(dto,tagConfig);
            await this._tagConfigRepository.Update(tagConfig);
        }


        /// <summary>
        /// 根据group 查询所有
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        public async Task<List<TagConfigDetailVO>> All(string group) {
            List<TagConfig> list = await this._tagConfigRepository.All(new TagConfigDetailQuery { GroupKey = group }.Exp());
            List<TagConfigDetailVO> ret = new List<TagConfigDetailVO>();
            list.ForEach(x => {
                ret.Add(x.Adapt<TagConfigDetailVO>());
            });
            return ret;
        }

    }
}
